package com.guli.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }
     @Override
    public List<CategoryEntity> treeList(){
         List<CategoryEntity> allList=baseMapper.selectList(null);
      List<CategoryEntity> liList=allList.stream().filter(item->{
          return item.getParentCid().equals(0L);
      }).map(l1->{
          List<CategoryEntity> entityList=getChildren(allList,l1);
          l1.setChildren(entityList);
          return l1;
      }).sorted((s1,s2)->{
          return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
      }).collect(Collectors.toList());
      return liList;
     }

      private List<CategoryEntity> getChildren(List<CategoryEntity> allList,CategoryEntity category){
     List<CategoryEntity> colList=   allList.stream().filter(item->{
            return item.getParentCid().equals(category.getCatId());
        }).map(cate->{
         List<CategoryEntity> entityList = getChildren(allList, cate);
         cate.setChildren(entityList);
         return cate;
     }).sorted((s1,s2)->{
         return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
     }).collect(Collectors.toList());
     return colList;
      }
//    @Override
//    public List<CategoryEntity> treeList() {
//        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_cid",0);
//        List<CategoryEntity> entities = baseMapper.selectList(wrapper);
//        for (CategoryEntity entity : entities) {
//            QueryWrapper<CategoryEntity> wrapper1 = new QueryWrapper<>();
//            wrapper1.eq("parent_cid",entity.getCatId());
//            List<CategoryEntity> list = baseMapper.selectList(wrapper1);
//            entity.setChildren(list);
//            for (CategoryEntity categoryEntity : list) {
//                QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("parent_cid",categoryEntity.getCatId());
//                List<CategoryEntity> entities1 = baseMapper.selectList(queryWrapper);
//                categoryEntity.setChildren(entities1);
//            }
//        }
//        return entities;
//    }


}